package feizhou.untils.tools;

import com.google.zxing.common.BitMatrix;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @ClassName: Base64Util
 * @Description 请描述下该类是做什么的
 * @Author feizhou
 * @Date 2023/10/24  11:05
 * @Verson 1.0
 **/
public class Base64Util {

    private static final int BUFFER_SIZE = 2 * 1024;
    private static final Logger log = LoggerFactory.getLogger(Base64Util.class);
    // base64图片前缀
    private static String image_pre = "data:image/jpg;base64,";

    /**
     * 图片转base64字符串
     *
     * @param imagePath 图片路径
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:23
     */
    public static String imageToBase64Str(String imagePath) {
        // 读取图片
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(imagePath);
            String base64Str = toBase64Str(fileInputStream);
            return image_pre + base64Str;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 图片转base64字符串
     *
     * @param bytes 输入流
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:33
     */
    public static String imageToBase64Str(byte[] bytes) {
        // 读取图片
        String base64Str = toBase64Str(bytes);
        if (base64Str != null) {
            return image_pre + base64Str;
        }
        return null;
    }

    /**
     * 图片转base64字符串
     *
     * @param inputStream 输入流
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:33
     */
    public static String imageToBase64Str(InputStream inputStream) {
        // 读取图片
        String base64Str = toBase64Str(inputStream);
        if (base64Str != null) {
            return image_pre + base64Str;
        }
        return null;
    }

//    /**
//     * <p>
//     * Title: ZipToBase64.java<／p>
//     * <p>
//     * Description: Base64 解密 zip 解压缩 <／p>
//     *
//     * @param path   解压文件夹路径 文件夹父文件可更改
//     * @param base64 base64加密字符
//     * @return Base64 压缩文件后进行base64编码
//     * @version 1.0
//     */

    /**
     * base
     *
     * @param base64
     * @param path
     * @return void
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:26
     */
    public static void base64ToFile(String base64, String path) throws RuntimeException {
        log.info("解压文件地址" + path);
        try {

            byte[] byteBase64 = Base64.decodeBase64(base64);
            ByteArrayInputStream byteArray = new ByteArrayInputStream(byteBase64);
            ZipInputStream zipInput = new ZipInputStream(byteArray);
            ZipEntry entry = zipInput.getNextEntry();
            File fout = null;
            File file = new File(path);
            String parent = file.getPath();
            while (entry != null && !entry.isDirectory()) {
                log.info("文件名称:    [{}]", entry.getName());
                fout = new File(parent, entry.getName());
                if (!fout.exists()) {
                    (new File(fout.getParent())).mkdirs();
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout));
                int offo = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((offo = zipInput.read(buffer)) != -1) {
                    bos.write(buffer, 0, offo);
                }
                bos.close();
                // 获取 下一个文件
                entry = zipInput.getNextEntry();
            }
            zipInput.close();
            byteArray.close();
        } catch (Exception e) {
            throw new RuntimeException("解压流出现异常", e);
        }

    }


    /**
     * 将base64字符解码，转换为文件
     *
     * @param base64CodeStr
     * @param targetPath
     * @return void
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:31
     */
    public static void toFile(String base64CodeStr, String targetPath) {
        byte[] buffer = Base64.decodeBase64(base64CodeStr);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(targetPath);
            out.write(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * base64字符串转为输入流
     *
     * @param base64Str
     * @return
     */
    public static InputStream toInputStream(String base64Str) {
        byte[] bytes = new byte[0];
        try {
            bytes = Base64.decodeBase64(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //转化为输入流
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        return inputStream;
    }

    /**
     * 字节流转为base64 字符串
     *
     * @param bytes
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:24
     */
    public static String toBase64Str(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    /**
     * 输入流转为base64 字符串
     *
     * @param inputStream
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:24
     */
    public static String toBase64Str(InputStream inputStream) {

        try {
            byte[] bytes = IOUtils.toByteArray(inputStream);
            String encoded = Base64.encodeBase64String(bytes);
            return encoded;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    /**
     * 将文件 转成base64字符串
     *
     * @param path
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/10/24 11:31
     */
    public static String toBase64Str(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        return toBase64Str(inputFile);
    }

    public static void main(String[] args) throws Exception {
        String imagePath = "C:\\Users\\26372\\Desktop\\temp\\1.png";
//        String s = Base64Util.imageToBase64Str(imagePath);
//        FileInputStream inputStream = new FileInputStream(imagePath);
//        String s = Base64Util.imageToBase64Str(inputStream);
//        System.out.println(s);
        String base64Str = "";

        String s1 = Base64Util.toBase64Str(imagePath);
        System.out.println(s1);
        String imagePath2 = "C:\\Users\\26372\\Desktop\\temp\\3.png";
        Base64Util.toFile(s1, imagePath2);

    }
}